import os
import requests
from openai import OpenAI

from config.model_config import get_base_open_ai, qwen_model_path, xin_qwen_model


def get_weather(city: str) -> dict:
    """调用OpenWeatherMap API获取天气数据"""
    return {
        "city": "北京",
        "temp": "fsdf",
        "humidity": "xx",
        "description": "fdsfdsfsfsd"
    }

# OpenAI工具定义
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "获取指定城市的天气信息",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "城市名称"}
            },
            "required": ["city"]
        }
    }
}]

# 示例调用
if __name__ == "__main__":
    client = get_base_open_ai()
    response = client.chat.completions.create(
        model=xin_qwen_model,
        messages=[{"role": "user", "content": "上海现在天气怎么样？"}],
        tools=tools,
        tool_choice="auto"
    )
    ## Choice(finish_reason='tool_calls', index=0, logprobs=None, message=ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=None, audio=None,
    # function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_48916845-a5c1-43cc-9445-915c40978aea',
    # function=Function(arguments='{"city": "上海"}', name='get_weather'), type='function')]))
    print(response.choices[0])
    if response.choices[0].message.tool_calls:
        tool_call = response.choices[0].message.tool_calls[0]
        if tool_call.function.name == "get_weather":
            import json

            args = json.loads(tool_call.function.arguments)
            print(get_weather(args["city"]))
